package cn.sima.plantmessage.user;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.impl.conn.DefaultClientConnection;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ContentResolver;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast;
import cn.sima.plantmessage.Conf;
import cn.sima.plantmessage.R;
import cn.sima.plantmessage.lib.toast.Crouton;
import cn.sima.plantmessage.lib.toast.Style;

public class RegisterActivity extends Activity implements OnClickListener {

	private EditText register_username;
	private EditText register_email;
	private EditText register_passwd;
	private EditText register_passwd_verify;
	private RadioGroup mSexGroup;
	private RadioButton sexbutton;
	private Button mJoin;
	private String picPath = null;
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_register);
		register_username = (EditText) findViewById(R.id.sinup_username);
		register_email = (EditText) findViewById(R.id.sinup_email);
		register_passwd = (EditText) findViewById(R.id.sinup_pwd);
		register_passwd_verify = (EditText) findViewById(R.id.sinup_pwd_again);
		mSexGroup = (RadioGroup) findViewById(R.id.sex_group);
		sexbutton = (RadioButton) findViewById(R.id.radio_girl);
		mSexGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				sexbutton = (RadioButton) findViewById(checkedId);
			}
		});
		mJoin = (Button) findViewById(R.id.btnJoin);
		mJoin.setOnClickListener(this);

		ActionBar ab = getActionBar();
		ab.setDisplayHomeAsUpEnabled(true);
		ab.setHomeButtonEnabled(true);
		ab.setTitle("注册");
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {

			// 关闭Activity
			finish();
			// overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
			overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);

			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * 显示ShortToast
	 */
	public void showCustomToast(String pMsg, int view_position) {
		// ToastUtil.createCustomToast(this, pMsg, Toast.LENGTH_SHORT).show();
		Crouton.makeText(this, pMsg, Style.CONFIRM, view_position).show();
		// Crouton.makeText(this, pMsg, Style.ALERT, view_position).show();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.btnJoin:
			String username = register_username.getText().toString();
			String email = register_email.getText().toString();
			String passwd = register_passwd.getText().toString();
			String passwd_verify = register_passwd_verify.getText().toString();
			String sex = (String) sexbutton.getText();
			if (TextUtils.isEmpty(email)) {
				Toast.makeText(getApplicationContext(), "邮箱不可以为空！",
						Toast.LENGTH_SHORT).show();
				return;
			}
			if (TextUtils.isEmpty(email)) {
				Toast.makeText(getApplicationContext(), "邮箱格式不对！",
						Toast.LENGTH_SHORT).show();
				return;
			}
			if (TextUtils.isEmpty(passwd)) {
				Toast.makeText(getApplicationContext(), "密码不可以为空！",
						Toast.LENGTH_SHORT).show();
				return;
			}
			if (!verifyPasswd(passwd, passwd_verify)) {
				Toast.makeText(getApplicationContext(), "两次密码不一",
						Toast.LENGTH_SHORT).show();
				return;
			}
//			String path = Conf.BASE_URL+"/user/addUser";
//			Map<String, String> params = new HashMap<String, String>();
//			params.put("name", username);
//			params.put("email", email);
//			params.put("passwd", passwd);
//			params.put("sex", sex);
//			params.put("age", "34");
//			params.put("phoneNumber", "424234");
//			String result = HttpUtils.sendHttpClientPost(path, params, "utf-8");
//			 Log.e("HttpUtils result",result);
			Ion.with(this)
			.load(Conf.BASE_URL+"/user/addUser")
			.setBodyParameter("name", username)
			.setBodyParameter("email", email)
			.setBodyParameter("passwd", passwd)
			.setBodyParameter("sex", sex)
//			.setBodyParameter("age", "34")
//			.setBodyParameter("phoneNumber", "424234")
			.asJsonObject()
			.setCallback(new FutureCallback<JsonObject>() {
				   @Override
				    public void onCompleted(Exception e, JsonObject result) {
					   Log.e("result",result.toString());
					   JsonElement je = result.get("success");
					   if(je.getAsBoolean()){
						   Toast.makeText(getApplicationContext(), "注册成功",
									Toast.LENGTH_SHORT).show();
						   Intent it = new Intent(getApplicationContext(), LoginActivity.class);
						   startActivity(it);
						   finish();
					   }else{
						   Toast.makeText(getApplicationContext(), "注册失败",
									Toast.LENGTH_SHORT).show();
					   }
				    }
				});
			break;
           
        default:
        	break;
		}
	}

	 
	private boolean verifyPasswd(String passwd, String passwd_verify) {
		if (passwd.equals(passwd_verify)) {
			return true;
		} else {
			return false;
		}
	}

}
